# -*- coding: utf-8 -*-

"""
备忘录模式

模式特点：在不破坏封装性的前提下捕获一个对象的内部状态，并在该对象之外保存这个状态，以后可以将对象恢复到这个状态。

程序实例：将Originator对象的状态封装成Memo对象保存在Caretaker内

代码特点：无
"""


class Originator(object):
    def __init__(self):
        self.state = ""

    def show(self):
        print self.state

    def create_memo(self):
        return Memo(self.state)

    def set_memo(self, memo):
        self.state = memo.state


class Memo(object):
    state = ""

    def __init__(self, ts):
        self.state = ts


class Caretaker(object):
    memo = ""

if __name__ == "__main__":
    on = Originator()
    on.state = "on"
    on.show()
    c = Caretaker()
    c.memo = on.create_memo()

    on.state = "off"

    on.show()

    on.set_memo(c.memo)

    on.show()